package com.zhoug.common3.drawable;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;

import com.zhoug.common3.utils.LogUtils;

/**
 * 阴影背景构建器
 *请使用的View设置padding,阴影边设置最小边距为{@link #getShadowWidth()}否则内容会占用阴影部分
 * @Author 35574
 * @Date 2020/11/4
 * @Description
 */
public class ShadowBuilder {
    private static final String TAG = ">>>ShadowBuilder";
    private static final boolean DEBUG = false;
    public static final int LEFT = 1;//0001
    public static final int TOP = 2;//0010
    public static final int RIGHT = 4;//0100
    public static final int BOTTOM = 8;//1000

    /***
     * 单个图层向内偏移的距离
     */
    private static int ONE_INSET = 2;
    /****
     * 阴影大小:越大使用的图层就越多
     * 默认10dp
     */
    private int shadowWidth;
    /**
     * 阴影的最大透明度:透明度递增的最大值
     */
    private int maxShadowAlpha=128;

    /***
     * 阴影的位置{@link #LEFT#TOP#RIGHT#BOTTOM}
     */
    private int position = BOTTOM;
    /**
     * 背景颜色
     */
    private int color = Color.WHITE;
    /**
     * 阴影颜色:设置0表示和背景色一样
     */
    private int shadowColor=Color.parseColor("#aaaaaa");

    /**
     * 角度半径
     * 8个长度的数组分别表示:左上角,右上角,右下角,左下角,每个角有2个半径表示两端
     */
    private float[] cornerRadii;


    public ShadowBuilder(Context context) {
        float density = context.getResources().getDisplayMetrics().density;
        //默认10dp
        shadowWidth = (int) (10 * density);
        ONE_INSET= (int) Math.min(density, 2);
        if (DEBUG) {
            LogUtils.d(TAG, "ShadowBuilder:ONE_INSET="+ONE_INSET);
        }
    }

    /**
     * 设置阴影的位置
     *
     * @param position {@link #LEFT#TOP#RIGHT#BOTTOM} 默认:BOTTOM
     * @return
     */
    public ShadowBuilder setPosition(int position) {
        this.position = position;
        return this;
    }

    /**
     * 设置阴影的最大透明度
     * @param maxShadowAlpha  (0-255)默认128
     * @return
     */
    public ShadowBuilder setMaxShadowAlpha(int maxShadowAlpha) {
        this.maxShadowAlpha = maxShadowAlpha;
        return this;
    }

    /**
     * 设置背景色
     *
     * @param color
     * @return
     */
    public ShadowBuilder setColor(int color) {
        this.color = color;
        return this;
    }

    /**
     * 设置阴影色
     *
     * @param shadowColor 设置0表示和背景色一样
     * @return
     */
    public ShadowBuilder setShadowColor(int shadowColor) {
        this.shadowColor = shadowColor;
        return this;
    }

    /**
     * 设置阴影大小
     *
     * @param shadowWidth 设置0表示和背景色一样 单位:px
     * @return
     */
    public ShadowBuilder setShadowWidth(int shadowWidth) {
        this.shadowWidth = shadowWidth;
        return this;
    }

    /**
     * 分别设置角度的半径
     *
     * @param cornerRadii 8个长度的数组分别表示:左上角,右上角,右下角,左下角,每个角有2个半径表示两端
     * @return
     */
    public ShadowBuilder setCornerRadii(float[] cornerRadii) {
        this.cornerRadii = cornerRadii;
        return this;
    }


    /**
     * 设置全部角度的半径
     *
     * @param corners
     * @return
     */
    public ShadowBuilder setCorners(float corners) {
        if (corners > 0) {
            cornerRadii = new float[]{corners, corners, corners, corners, corners, corners, corners, corners};
        } else {
            cornerRadii = null;
        }
        return this;
    }

    /**
     * 只设置左边角度的半径
     *
     * @param corners
     * @return
     */
    public ShadowBuilder setLeftCorners(float corners) {
        if (corners > 0) {
            cornerRadii = new float[]{corners, corners, 0, 0, 0, 0, corners, corners};
        } else {
            cornerRadii = null;
        }
        return this;
    }

    /**
     * 只设置右边角度的半径
     *
     * @param corners
     * @return
     */
    public ShadowBuilder setRightCorners(float corners) {
        if (corners > 0) {
            cornerRadii = new float[]{0, 0, corners, corners, corners, corners, 0, 0};
        } else {
            cornerRadii = null;
        }
        return this;
    }

    /**
     * 只设置上边角度的半径
     *
     * @param corners
     * @return
     */
    public ShadowBuilder setTopCorners(float corners) {
        if (corners > 0) {
            cornerRadii = new float[]{corners, corners, corners, corners, 0, 0, 0, 0};
        } else {
            cornerRadii = null;
        }
        return this;
    }

    /**
     * 只设置下边角度的半径
     *
     * @param corners
     * @return
     */
    public ShadowBuilder setBottomCorners(float corners) {
        if (corners > 0) {
            cornerRadii = new float[]{0, 0, 0, 0, corners, corners, corners, corners};
        } else {
            cornerRadii = null;
        }
        return this;
    }

    public int getShadowWidth() {
        return shadowWidth;
    }

    /**
     *
     * @param radio
     * @return
     */
    private int getShadowColor(float radio){
        int color=shadowColor != 0 ? shadowColor : this.color;
        int alpha= (int) (radio*maxShadowAlpha);
        return Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
    }

    public LayerDrawable build() {
        //根据阴影大小计算出需要的图层数
        int layerCount = shadowWidth / ONE_INSET;
        //所有图层数
        int drawableLength=layerCount+1;
        if (DEBUG) {
            LogUtils.d(TAG, "build:drawableLength="+drawableLength);
        }
        Drawable[] drawables = new Drawable[drawableLength];
        GradientDrawable drawable;
        //初始化阴影图层设置颜色和角度
        for (int i = 0; i < layerCount; i++) {
            drawable = new GradientDrawable();
            drawables[i] = drawable;
            if (cornerRadii != null) {
                drawable.setCornerRadii(cornerRadii);
            }
            drawable.setColor(getShadowColor((i+1)/(float)layerCount));
//            drawable.setColor(shadowColor != 0 ? shadowColor : color);
            //透明度递增最大为200
//            drawable.setAlpha((int) (maxShadowAlpha * (((i + 1)) / (float) layerCount)));
        }
        //最后一个是背景
        drawable = new GradientDrawable();
        drawable.setColor(color);
        if (cornerRadii != null) {
            drawable.setCornerRadii(cornerRadii);
        }
        drawables[drawableLength- 1] = drawable;

        //设置图层的间距实现阴影
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        int left, top, right, bottom;
        for (int i = 1; i < drawableLength; i++) {
            left = 0;
            top = 0;
            right = 0;
            bottom = 0;
            if ((position & LEFT) == LEFT) {
                left = i * ONE_INSET;
            }else if(i==drawableLength-1){
                //防止圆角时,漏出阴影
                left-=1;
            }
            if ((position & TOP) == TOP) {
                top = i * ONE_INSET;
            }else if(i==drawableLength-1){
                top-=1;
            }
            if ((position & RIGHT) == RIGHT) {
                right = i * ONE_INSET;
            }else if(i==drawableLength-1){
                right-=1;
            }
            if ((position & BOTTOM) == BOTTOM) {
                bottom = i * ONE_INSET;
            }else if(i==drawableLength-1){
                bottom-=1;
            }
            layerDrawable.setLayerInset(i, left, top, right, bottom);
        }

        //设置padding否则内容可能在阴影上,设置了后xml中的padding无效,还是让使用者自己设置padding
//        int paddingLeft=0,paddingTop=0,paddingRight=0,paddingBottom=0;
//        if ((position & LEFT) == LEFT) {
//            paddingLeft=shadowWidth;
//        }
//        if ((position & TOP) == TOP) {
//            paddingTop=shadowWidth;
//        }
//        if ((position & RIGHT) == RIGHT) {
//            paddingRight=shadowWidth;
//        }
//        if ((position & BOTTOM) == BOTTOM) {
//            paddingBottom=shadowWidth;
//        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            layerDrawable.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
//        }
        return layerDrawable;
    }



}
